import {loginAPI} from "@/api/user"
import {getProfileApi} from '@/api/permission'

export default {
  namespaced:true,
  state(){
    return{
      token:'',
      profile:null
    }
  },
  mutations: {
    setProfile(state,profile){
      state.profile = profile
    },
    setToken(state,token){
      state.token = token
    },
    clearToken(state){
      state.token = ''
      state.profile = null
      localStorage.removeItem("account");
    }
  },
  actions: {
    async getProfileAction(ctx){
      const res = await getProfileApi()
      ctx.commit('setProfile',res.data)
      return res.data.permissions
    },
    async loginAction(ctx,info){
      let res = await loginAPI(info)
      console.log(res.data)
      ctx.commit('setToken',res.data.token)
    }
  }
}
